<?php
    include_once ('models/FileUpload_model.php');
    include_once ('controllers/backend_controller.php');
class FileUploads_Controller extends Backend_Controller{
    public $model;
    public $action = 'listcat';
    public function __construct(){
        parent::__construct();
        // Create Model class
        $this->model = new FileUpload_Model();
        // Control action
        if(isset($_GET['action'])) $this->action = $_GET['action'];
        $this->{$this->action}();
    }
    public function lists(){
        $list = $this->model->getLists();
        include 'views/FileUploads/lists.php';
    }
    public function addCat(){
        $addCat = new FileUpload_Model();
        if(isset($_POST['btn_submit'])){
            if(!empty($_POST['catname'])){
                $catname = $_POST['catname'];
                $des = $_POST['des'];
                $this -> model -> addCat($catname,$des);
                $this -> action = 'listcat';
                $this->{$this->action}();
            }
        }
        else{
            include 'views/FileUploads/addCat.php';
        }
    }
    public function listcat(){
        $listcat = $this->model->getListCat();
        include 'views/FileUploads/listcat.php';
    }
    public function delcat(){
        $id = $_GET['id'];
        $this -> model -> delcat($id);
        $this -> action = 'listcat';
        $this->{$this->action}();
    }
    public function editcat(){
        $id = $_GET['id'];
        if(isset($_POST['btn_submit'])){
            $catname = $_POST['catname'];
            $des = $_POST['des'];
            $this -> model ->editcat($id,$catname,$des);
            $this -> action = 'listcat';
            $this->{$this->action}();
        }
        $view = $this -> model -> viewcat($id);
        include 'views/FileUploads/viewcat.php';
    }
    public function upload(){
        $upload = new FileUpload_Model();
        $cat = $upload -> getListCat();
        if(isset($_POST['btn_submit'])){
            if((!empty($_POST['des']))){
                $des = $_POST['des'];
                $id_catfile = $_POST['catfile'];
                    if($_FILES['up']['name']!= null){
                        move_uploaded_file($_FILES['up']['tmp_name'],"upload/".$_FILES['up']['name']);
                        $filename = $_FILES['up']['name'];
                        $link = 'upload/'.$filename;
                    }
                $this -> model -> upload($filename,$des,$link,$id_catfile);
                $this -> action = 'lists';
                $this->{$this->action}();
                }
            }
        include_once 'views/FileUploads/upload.php';
        }
    }